package com.wxy;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxy.dao.UserMapper;
import com.wxy.domain.User;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.apache.ibatis.session.RowBounds;
import org.apache.logging.log4j.core.net.MailManagerFactory;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.MailMessage;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
public class test {
    @Autowired
    private RestHighLevelClient client;
    @Autowired
    private RabbitTemplate template;
    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    private UserMapper userMapper;
    /*private final JavaMailSender mailSender;
    @Autowired
    public test(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }*/

    public static void println(Object message){
        System.out.println(message);
    }
    @Test
    public void test1() throws IOException {
        IndexRequest indexRequest = new IndexRequest("wxy").id("1");
        User user = new User(18,"张三","111111");
        indexRequest.source(JSONUtil.toJsonStr(user), XContentType.JSON);
        client.index(indexRequest, RequestOptions.DEFAULT);


        UpdateRequest updateRequest = new UpdateRequest("wxy", "1");
        updateRequest.doc(
                "name","张氏",
                "age",17
        );
        client.update(updateRequest, RequestOptions.DEFAULT);


        DeleteRequest deleteRequest = new DeleteRequest("wxy", "1");
        client.delete(deleteRequest, RequestOptions.DEFAULT);


        GetRequest getRequest = new GetRequest("wxy", "1");
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(getResponse.getSourceAsString());


        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.add(new DeleteRequest("wxy", "1"));
        bulkRequest.add(new UpdateRequest("wxy", "2"));
        client.bulk(bulkRequest, RequestOptions.DEFAULT);


        SearchRequest searchRequest = new SearchRequest("wxy");
        searchRequest.source().query(QueryBuilders.matchAllQuery());
        searchRequest.source().query(QueryBuilders.termQuery("name","张氏"));
        searchRequest.source().query(QueryBuilders.matchQuery("name","李四"));
        searchRequest.source().query(QueryBuilders.rangeQuery("age").gte(18));
        searchRequest.source().sort("age", SortOrder.ASC);
        searchRequest.source().from(2);
        searchRequest.source().size(20);
        searchRequest.source().query(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("age", 18)));
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        SearchHit[] hit=hits.getHits();
        for (SearchHit h : hit) {
            String jsonString = h.getSourceAsString();
            User user1 = JSONUtil.toBean(jsonString, User.class);
            System.out.println(user1);
        }
    }
    @Test
    public void RabbitTemplateTest() {
        Map<String, Object> params = new HashMap<>();
        params.put("zhangsan","111111");
        template.convertAndSend("wxy.direct", "hello", params);
    }

    @Test
    public void EmailTest(){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("3435099467@qq.com");
        message.setTo("3161484309@qq.com");
        message.setSubject("这是一封邮件");
        message.setText("你好,张庭豪");
        mailSender.send(message);
    }

    @Test
    public void EmailDetailTest() throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(mimeMessage);
        helper.setFrom("3435099467@qq.com");
        helper.setTo("478685194@qq.com");
        helper.setSubject("邮件");
        helper.setText("hello"+"<img src='https://img.shetu66.com/2023/04/25/1682391068745168.png'/>");
        mailSender.send(mimeMessage);
    }

    @Test
    public void MapperTest(){
        userMapper.update(new UpdateWrapper<User>().eq("id",1).set("name","张氏"));
        User user=new User(1,"张氏","111111");
        userMapper.update(user,new UpdateWrapper<User>().eq("id",1));
    }

    @Test
    public void ImgTest() throws Exception {
        String path="D:/代码/springboot_Demo1/img/img1.jpeg";
        InputStream inputStream=new FileInputStream(path);
        byte[] bytes=inputStream.readAllBytes();
        String string= Base64.getEncoder().encodeToString(bytes);
        System.out.println(string);
    }
    @Test
    public void test(){
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss,SSS");
        System.out.println(localDateTime.format(dateTimeFormatter));
    }
    @Test
    public void pageTest(){
        Page page = new Page(2,2);
        IPage page1 = userMapper.selectPage(page,null);
        page1.getRecords().forEach(System.out::println);
    }
}
